package com.www.jpa.control;

import com.www.jpa.service.DatabaseBackupService;
import com.www.jpa.service.DatabaseRestoreService;
import io.swagger.annotations.Api;
import java.util.Collections;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 * .
 *
 * @author o_o
 * @date 2025-06-23
 */
@Api("backup")
@RestController
@RequestMapping("/api/backup")
public class BackupController {

    @Autowired
    private DatabaseBackupService backupService;

    @Autowired
    private DatabaseRestoreService restoreService;

    @PostMapping("/create")
    public ResponseEntity<?> createBackup(
            @RequestParam String backupName,
            @RequestBody List<String> tables) {

        Long backupId = backupService.performBackup(backupName, tables);
        return ResponseEntity.ok(Collections.singletonMap("backupId", backupId));
    }

    @PostMapping("/restore/{backupId}")
    public ResponseEntity<?> restoreBackup(@PathVariable Long backupId) {
        restoreService.performRestore(backupId);
        return ResponseEntity.ok().build();
    }
}